// $Id: GroupPropertyPage.java 31 2011-09-21 17:36:34Z huijzer@gmail.com $
package lime.group.editor;

import lime.Activator;
import lime.common.LimeException;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.documentum.fc.client.IDfGroup;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;

public class GroupPropertyPage extends FormPage {

	private IDfGroup group;
	private FormToolkit toolkit;
	private ScrolledForm scrolledForm;
	IManagedForm managedForm;

	// Services
	private RepositoryService repositoryService;

	public GroupPropertyPage(FormEditor editor) {
		super(editor, "properties", "Properties");
		
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
		
		GroupInput input = (GroupInput) editor.getEditorInput();
		IDfId groupId = new DfId(input.getGroup().getObjectId());
		try {
			this.group = (IDfGroup) repositoryService.getSession().getObject(groupId);
		} catch (DfException e) {
			throw new LimeException(e);
		}
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {

		this.managedForm = managedForm;
		
		scrolledForm = managedForm.getForm();
		scrolledForm.setText("Group Properties");
		Image image = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/property.png")
				.createImage();
		scrolledForm.setImage(image);

		toolkit = managedForm.getToolkit();
		toolkit.decorateFormHeading(scrolledForm.getForm());

		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 2;
		layout.makeColumnsEqualWidth = true;
		scrolledForm.getBody().setLayout(layout);

		try {

			// ====================
			// BASIC SECTION
			// ====================

			Composite basicContainer = this.createSection("Basic", 1, 2);

			this.createField(basicContainer, "Object ID:", group.getObjectId().getId());
			this.createField(basicContainer, "Group Name:", group.getGroupName());
			this.createField(basicContainer, "Description:", group.getDescription());
			this.createField(basicContainer, "Group Class:", group.getGroupClass());

			// ====================
			// GROUP SECTION
			// ====================

			Composite groupsContainer = this.createSection("Groups", 1, 1);

			Label groupsLabel = toolkit
					.createLabel(groupsContainer, "The names of groups that are users in the group.");
			groupsLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));

			GridData layoutData = new GridData(GridData.FILL_BOTH);
			layoutData.heightHint = 150;
			layoutData.horizontalSpan = 2;

			ListViewer groupViewer = new ListViewer(groupsContainer, SWT.V_SCROLL);
			groupViewer.getList().setLayoutData(layoutData);
			groupViewer.getList().setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);

			for (int i = 0; i < group.getGroupsNamesCount(); i++) {
				String groupName = group.getGroupsNames(i);
				groupViewer.getList().add(groupName);
			}

			// ====================
			// USER SECTION
			// ====================

			Composite usersContainer = this.createSection("Users", 1, 1);

			Label usersLabel = toolkit.createLabel(usersContainer,
					"The names of the users directly contained in the group.");
			usersLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));

			ListViewer userViewer = new ListViewer(usersContainer, SWT.V_SCROLL);
			userViewer.getList().setLayoutData(layoutData);
			userViewer.getList().setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);

			for (int i = 0; i < group.getUsersNamesCount(); i++) {
				String userName = group.getUsersNames(i);
				userViewer.getList().add(userName);
			}

		} catch (DfException e) {
			throw new LimeException(e);
		}

	}

	public void createField(Composite container, String label, String text) {
		toolkit.createLabel(container, label);
		Text textWidget = toolkit.createText(container, text);
		textWidget.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		textWidget.setEditable(false);
	}

	private Composite createSection(String text, int colspan, int rowspan) {

		Composite body = scrolledForm.getBody();

		SectionPart sectionPart = new SectionPart(body, toolkit, Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);
		Section section = sectionPart.getSection();
		section.setText(text);

		TableWrapData layoutData = new TableWrapData(TableWrapData.FILL_GRAB);
		layoutData.colspan = colspan;
		layoutData.rowspan = rowspan;
		section.setLayoutData(layoutData);

		Composite sectionClient = toolkit.createComposite(section);
		sectionClient.setLayout(new GridLayout(2, false));
		section.setClient(sectionClient);

		toolkit.paintBordersFor(sectionClient);

		managedForm.addPart(sectionPart);
		
		return sectionClient;
	}
}